<template>
	<view class="page ex-mall-tab-page" >
		<view class="top" >
			<!-- #ifdef MP -->
			<view class="avatar">
				<open-data type="userAvatarUrl"></open-data>
			</view>
			<view class="name"><open-data type="userNickName"></open-data></view>
			<!-- #endif -->
			<view class="mine">
				<view class="mine-item">
					<image src="/static/images/mine_rec.png"></image>
					<view class="mine-item-title">回收物品</view>
					<view class="mine-item-number">{{garbageWeight}}<span style="font-size: 22upx;margin-left: 6upx;">千克</span></view>
				</view>
				<view class="mine-item">
					<image src="/static/images/mine_open.png"></image>
					<view class="mine-item-title">减少碳排放</view>
					<view class="mine-item-number">{{carbonneutral}}<span style="font-size: 22upx;margin-left: 6upx;">千克</span></view>
				</view>
				<view class="mine-item">
					<image src="/static/images/mine_pro.png"></image>
					<view class="mine-item-title">保护树木</view>
					<view class="mine-item-number">{{saveTrees}}<span style="font-size: 22upx;margin-left: 6upx;">棵</span></view>
				</view>
			</view>
		</view>
		
		<view class="content">
			<view class="list">
				<view class="item" @click="gotoPage('order/order-log')">
					<view class="left">
						<!-- <view class="icon-box">
							<image style="margin-top: -8px;" src="/static/images/mine_rec.png"></image>
						</view> -->
						<view class="title">收益明细</view>
					</view>
					<view class="right">
						<view class="arrow">
							<text class="icon ri-arrow-right-s-line"></text>
						</view>
					</view>
				</view>
				<view class="item" @click="gotoPage('order/address')">
					<view class="left">
						<!-- <view class="icon-box">
							<image src="/static/images/mine_pro.png"></image>
						</view> -->
						<view class="title">地址管理</view>
					</view>
					<view class="right">
						<view class="arrow">
							<text class="icon ri-arrow-right-s-line"></text>
						</view>
					</view>
				</view>
				<button class="button" open-type="getUserInfo" @getuserinfo="setUser" style="margin-top: 20px;">更新用户信息</button>
				<button class="button" open-type="contact" style="margin-top: 20px;">联系客服</button>
				<!-- 
				<view class="item" >
					<view class="left">
						<view class="icon-box">
							<text class="icon ri-settings-5-line"></text>
						</view>
						<view class="title">我的设置</view>
					</view>
					<view class="right">
						<view class="arrow">
							<text class="icon ri-arrow-right-s-line"></text>
						</view>
					</view>
				</view> -->
			</view>
		</view>
	</view>
</template>

<script>
	import {mapGetters} from 'vuex'
	export default {
		computed: {
			...mapGetters({
				hasLogin:'hasLogin',
				userInfo: 'userInfo'
		   })
		},
		data(){
			return {
				carbonneutral:0,
				garbageWeight:0,
				saveTrees:0,
				typeList:getApp().globalData.GarbageType
			}
		},
		onShareTimeline: function() {
		  	return {
				path: '/pages/user/user?invitation='+this.userInfo._id
		  	}
		},
		onShow() {
			this.$api("recovery-order","getOrderLog").then(res=>{
				let carbonneutral=0,garbageWeight=0,saveTrees=0
				res.data.forEach(v=>{
					carbonneutral+=this.typeList[v.garbageType].carbonneutral*(v.weight||20)
					saveTrees+=carbonneutral/6
					garbageWeight+=Number(v.weight||20)
				})
				this.carbonneutral=Number(carbonneutral).toFixed(2)
				this.garbageWeight=Number(garbageWeight).toFixed(2)
				this.saveTrees=Number(saveTrees).toFixed(2)
			})
		},
		onLoad() {
			//#ifdef MP
			wx.showShareMenu({
			  withShareTicket: true,
			  menus: ['shareAppMessage', 'shareTimeline']
			})
			//#endif
		},
		methods: {
			gotoPage(e){
				uni.navigateTo({
					url:"/pages/"+e
				})
			},
			setUser(info){
				let {avatarUrl,nickName}=info.target.userInfo
				this.userInfo.nickName=nickName
				this.userInfo.avatarUrl=avatarUrl
				console.log({avatarUrl,nickName})
				this.$api('user-center','updateUser',{avatarUrl,nickName}).then(res=>{
					this.$store.commit('user/login', this.userInfo)
					uni.navigateTo({
						url:'./userinfo'
					})
				})
			},
			goAbout() {
				uni.navigateTo({
					url: '/pages/about/index'
				});
			},
			getintegral(){
				uni.showModal({
					title: '获取积分',
					content: '转发小程序给微信好友或微信群，好友点击使用(登录授权)后即算邀请成功；成功邀请1位好友可获得100积分等同于1元，邀请好友及获取积分无上限。',
					showCancel: false
				});
			}
		}
	}
</script>

<style lang="scss">
	page{
		height: 100%;
		overflow-x: hidden;
	}
	.mine{
		display: flex;
		width: 100%;
		justify-content:center;
		align-items: center;
		background: #FFFFFF;
		text-align: center;
		padding: 20upx;
		.mine-item{
			flex: 1;
		}
		.mine-item image{
			width: 100upx;
			height: 100upx;
		}
		.mine-item-title{
			color: #9C9C9A;
			font-size: 30upx;
			padding: 10upx 0;
		}
		.mine-item-number{
			color: #31b977;
			font-size: 36upx;
			font-weight: 500;
		}
	}
	
	.button{
		margin-bottom:20px ;
		background-image: linear-gradient(-90deg, 
		#3da180 0%, 
		#62c980 100%), 
		linear-gradient(
		#e8ecef, 
		#e8ecef);
		background-blend-mode: normal, 
		normal;
		border-radius: 25px;
		
		height: 45px;
		line-height: 45px;
		font-family: PingFangSC-Medium;
		font-size: 14px;
		letter-spacing: 0px;
		color: #ffffff;
	}
	.page {
		height: 100%;
		overflow-x: hidden;
		background-color: #F7F7F7;
		&>.top {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 10px 0 10px 0;
			border-radius: 0 0 2em 2em;
			background-color: white;
			&>.avatar {
				width: 108px;
				height: 108px;
				overflow: hidden;
				border-radius: 50%;
				background-color: #E5AD8B;
			}

			&>.name {
				margin-top: 24px;
				font-size: 17px;
				font-weight: 600;
			}

			&>.label {
				margin-top: 18px;
				font-size: 14px;
			}
		}

		&>.content {
			&>.list {
				padding: 0 20px;

				&>.item {
					display: flex;
					justify-content: space-between;
					align-items: center;
					height: 50px;
					    margin-top: 10px;
					padding: 0 24px;
					background-color: #FFFFFF;
					border-radius: 40px;

					&>.left {
						display: flex;
						align-items: center;

						&>.icon-box {
							display: flex;
							justify-content: center;
							align-items: center;
							width: 30px;
							height: 30px;
							border-radius: 12px;
							&>image{
								width: 30px;
								height: 30px;
							}
							&>.icon {
								font-size: 18px;
								color: #FFFFFF;
							}
						}

						&>.title {
							margin-left: 18px;
							font-size: 13px;
							font-weight: 600;
							color: #222222;
						}
					}

					&:nth-child(3n-2) {
						&>.left {
							&>.icon-box {
								background-color: #FFC592;
							}
						}
					}

					&:nth-child(3n-1) {
						&>.left {
							&>.icon-box {
								background-color: #F76842;
							}
						}
					}

					&:nth-child(3n) {
						&>.left {
							&>.icon-box {
								background-color: #0897BD;
							}
						}
					}

					&>.right {
						display: flex;
						align-items: center;

						&>.arrow {
							display: flex;
							align-items: center;

							&>.icon {
								font-size: 18px;
								font-weight: 600;
								color: #9093A2;
							}
						}
					}
				}
			}
		}
	}
	.head{
		background-color: #FFFFFF;
		padding: 40upx;
		margin-bottom: 30upx;
		display: flex;
		flex-direction: row;
		align-items: center
	}
	.head-picture{
		width: 100upx;
		height: 100upx;
		background: #fff;
		border: 5upx solid #fff;
		border-radius: 50%;
		overflow: hidden;
	}
	.nickname{
		margin-left: 30upx;
		font-size: 36upx;
		font-weight: 500;
		text-align: left;
	}
</style>